#include "savesettings.h"

SaveSettings::SaveSettings(QWidget *parent) :
    QWidget(parent)
{
    m_gridLayout = new QGridLayout(this);

//    m_systemLogPathLabel = new QLabel("系统日志目录", this);
//    m_systemLogPathButton = new QPushButton("chioce path", this);
//    m_systemLogPathEdit = new QLineEdit(this);
//    m_systemLogPathCheckBox = new QCheckBox("start", this);

    // 初始化标签
    m_fileSaveWay = new QLabel("单个文件最大内存(MB):", this);
    m_fileSaveNum = new QLabel("文件存储个数上限:", this);

    // 初始化下拉框
    m_deleteWay = new QComboBox(this);  //文件删除方式

    // 初始化复选框
    m_exitAutoSave = new QCheckBox("退出软件自动保存", this);

    // 初始化数字选择框
    m_saveMaxNum = new QSpinBox(this);
    m_saveMaxNum->setRange(1, 65535);

    // 初始化输入框
    m_saveWayValueLine = new QLineEdit(this);

    // 初始化按钮
    m_saveParam = new QPushButton("保存并应用", this);

    m_savePathPushButton = new QPushButton("选择文件保存目录", this); //save path

    m_savePathLineEdit = new QLineEdit(this);  //saveLine show

    setControlValue();  //设置值
    loadConfig();       //加载配置文件
    setupLayout();      //设置布局
    setupConnections(); //设置连接
}

SaveSettings::~SaveSettings()
{
    //不需要手动析构控件，因为我构造函数加入了this参数，qt的树会自动释放所有子类

    //退出软件自动保存功能
    if(m_exitAutoSave->isChecked()) {
        saveConfig();
    }
}

void SaveSettings::setupLayout()
{
    //第0行，save log path
//    m_gridLayout->addWidget(m_systemLogPathLabel, 0, 0, 1, 1);
//    m_gridLayout->addWidget(m_systemLogPathEdit, 0, 1, 1, 1);
//    m_gridLayout->addWidget(m_systemLogPathButton, 0, 2, 1, 1);
//    m_gridLayout->addWidget(m_systemLogPathCheckBox, 0, 3, 1, 1);

//    m_systemLogPathEdit->setMaximumWidth(500);
//    m_systemLogPathButton->setMaximumWidth(200);

    //第一行，存储方式
    m_gridLayout->addWidget(m_fileSaveWay, 0, 0, 1, 1);
    m_gridLayout->addWidget(m_saveWayValueLine, 0, 1, 1, 1);

    m_saveWayValueLine->setMaximumWidth(500);

    //第二行，存储上限
    m_gridLayout->addWidget(m_fileSaveNum, 1, 0, 1, 1);
    m_gridLayout->addWidget(m_saveMaxNum, 1, 1, 1, 1);
    m_gridLayout->addWidget(m_deleteWay, 1, 2, 1, 1);
    m_saveMaxNum->setMaximumWidth(500);

    //第四行，退出保存
    m_gridLayout->addWidget(m_savePathPushButton, 4, 0, 1, 1);
    m_gridLayout->addWidget(m_savePathLineEdit, 4, 1, 1, 1);
    m_gridLayout->addWidget(m_saveParam, 4, 2, 1, 1);
    m_gridLayout->addWidget(m_exitAutoSave, 4, 3, 1, 1);

    m_savePathLineEdit->setMaximumWidth(500);
    m_exitAutoSave->setMaximumWidth(200);
    m_saveParam->setMaximumWidth(200);
    m_savePathPushButton->setMaximumWidth(200);
}

void SaveSettings::setupConnections()
{
    //按照大小分割
//    connect(m_saveWayValueLine, &QLineEdit::textEdited, [=]{

//    });

    //连接存储上限的spin改变信号
//    connect(m_saveMaxNum, &QSpinBox::editingFinished, [this]{

//    });

    //保存按钮
    connect(m_saveParam, &QPushButton::clicked, [=]{
       QMessageBox::information(this,
                              "提示",
                              "配置已生效",
                              QMessageBox::Ok);
       sendSigToSaveData();
       saveConfig();
    });

    connect(m_savePathPushButton, &QPushButton::clicked, [=]{
        QString path = QFileDialog::getExistingDirectory(
            this,
            tr("choice dir"),
            QDir::homePath(),
            QFileDialog::ShowDirsOnly);

        if(!path.isEmpty()) {
            m_savePathLineEdit->setText(path);
        }
    });

//    connect(m_systemLogPathButton, &QPushButton::clicked, [=]{
//        QString path = QFileDialog::getExistingDirectory(
//            this,
//            tr("choice dir"),
//            QDir::homePath(),
//            QFileDialog::ShowDirsOnly);

//        if(!path.isEmpty()) {
//            m_systemLogPathEdit->setText(path);
//        }
//    });
}

void SaveSettings::setControlValue()
{
    //文件上限删除方式
    QStringList deleteWays = {"超过上限后删除最旧文件", "超过上限后删除最新文件"};
    m_deleteWay->addItems(deleteWays);

    //默认退出自动保存
    m_exitAutoSave->setChecked(true);

    //默认文件分割方式选择
    m_saveWayValueLine->setPlaceholderText("请输入文件大小(单位M)");

    m_savePathLineEdit->setText("config/saveSettings.ini");
    m_savePathLineEdit->setReadOnly(true);

    //m_systemLogPathButton->setText("log/")
}

void SaveSettings::saveConfig() {
   QDir appDir = QCoreApplication::applicationDirPath();
   QString dir = QDir(appDir).filePath("config/saveSettings.ini");
   QSettings settings(dir, QSettings::IniFormat);

    //保存文件分割大小
    settings.setValue("SplitWay/bytes", m_saveWayValueLine->text().toInt());

    // 保存文件上限删除配置
    settings.setValue("DeleteWay/currentIndex", m_deleteWay->currentIndex());

    // 保存文本输入
    settings.setValue("SaveWayValue/text", m_saveMaxNum->text());

    // 保存复选框状态
    settings.setValue("ExitAutoSave/checked", m_exitAutoSave->isChecked());

    //save savepath
    settings.setValue("SavePath/path", m_savePathLineEdit->text());

    settings.sync(); // 立即写入文件
}

void SaveSettings::loadConfig() {
    QDir appDir = QCoreApplication::applicationDirPath();
    QString dir = QDir(appDir).filePath("config/saveSettings.ini");
    QSettings settings(dir, QSettings::IniFormat);

    // 加载下拉框选项
    m_deleteWay->setCurrentIndex(settings.value("DeleteWay/currentIndex", 0).toInt());

    //加载文件分割方式配置
    m_saveWayValueLine->setPlaceholderText("请输入文件大小(单位M)");
    m_saveWayValueLine->setText(settings.value("SplitWay/bytes", "512").toString());

    // 加载文本
    m_saveMaxNum->setValue(settings.value("SaveWayValue/text", "100").toInt());

    // 加载复选框
    m_exitAutoSave->setChecked(settings.value("ExitAutoSave/checked", true).toBool());

    //load save path
    m_savePathLineEdit->setText(settings.value("SavePath/path", "./data").toString());
}

void SaveSettings::sendSigToSaveData()
{
    // 文件分割每个文件大小(MB)
    m_settingsData.mSize = m_saveWayValueLine->text().toInt();

    // 文件存储上限
    m_settingsData.saveMaxNum = m_saveMaxNum->text().toInt();
    m_settingsData.deleteWayIndex = m_deleteWay->currentIndex();

    m_settingsData.fileSavePath = m_savePathLineEdit->text();

    emit sigSettingsData(m_settingsData);
}

const SettingsData &SaveSettings::getSettingsData()
{
    return m_settingsData;
}

